Skip to content

driver/redis: bound the connection handshake by DialTimeout (v1.6.0, Refs #1205) - #1209

Merged
FumingPower3925 merged 4 commits into
mainfrom
fix/redis-handshake-dial-timeout-1205
Nov 1, 2026
Merged

FumingPower3925 merged 4 commits into
mainfrom
fix/redis-handshake-dial-timeout-1205

Conversation

@FumingPower3925

Copy link
Copy Markdown
Contributor

driver/redis: bound the connection handshake by DialTimeout. Refs #1205 (found by the review of #1154; the code is older than that PR).

The problem

DialTimeout only covered net.Dialer.DialContext. The handshake (HELLO, then AUTH/SELECT) ran on the caller's context, so a node that accepts TCP and never answers HELLO held ClusterClient.Subscribe, ClusterClient.SSubscribe, Client.Subscribe and any Do that needed a new conn until the caller's context ended: for ever under context.Background(). (The reconnect loop was not affected: it bounds its own dial with a 5 s context.)

The change

redisConn.handshake runs under context.WithTimeout(ctx, DialTimeout) (5 s when unset). All three dial paths end in it (dialRedisConn on unix, dialRedisConnNetConn, dialDirectRedisConn), and the pool's command and pub/sub dials use those, so Do, Pipeline, the cluster node clients and the sentinel clients are bounded the same way. Nothing else in driver/redis runs a handshake (grep 'handshake(' driver/redis: the three callers). Doc of DialTimeout, WithDialTimeout, ClusterConfig.DialTimeout and SentinelConfig.DialTimeout updated.

Commits: 3edf21c the tests (RED on main), 361a5a7 the fix.

RED / GREEN

RED, 3edf21c alone (macOS arm64, go test ./driver/redis -run 1205 -v): the three dial paths (direct, eventloop, netconn), Client.Subscribe, Client.Do, ClusterClient.Subscribe and ClusterClient.SSubscribe all report still blocked after 2s: the handshake is not bounded by DialTimeout (test code uses a context without deadline and a 2 s watchdog, so main fails with a message, not a 10 min timeout). TestDialHandshakeKeepsTheCallersShorterDeadline1205 passes on main by design: it guards that a caller's shorter deadline still wins.

--- FAIL: TestDialHandshakeIsBoundedByDialTimeout1205 (0.00s)
    --- FAIL: TestDialHandshakeIsBoundedByDialTimeout1205/eventloop (2.00s)
    --- FAIL: TestDialHandshakeIsBoundedByDialTimeout1205/netconn (2.00s)
    --- FAIL: TestDialHandshakeIsBoundedByDialTimeout1205/direct (2.00s)
--- PASS: TestDialHandshakeKeepsTheCallersShorterDeadline1205 (0.20s)
--- FAIL: TestClientSubscribeAndDoBoundTheHandshake1205 (0.00s)
    --- FAIL: TestClientSubscribeAndDoBoundTheHandshake1205/Do (2.00s)
    --- FAIL: TestClientSubscribeAndDoBoundTheHandshake1205/Subscribe (2.00s)
--- FAIL: TestClusterSubscribeBoundsTheHandshake1205 (0.00s)
    --- FAIL: TestClusterSubscribeBoundsTheHandshake1205/Subscribe (2.00s)
    --- FAIL: TestClusterSubscribeBoundsTheHandshake1205/SSubscribe (2.00s)
FAIL
FAIL	github.com/goceleris/celeris/driver/redis	6.392s
FAIL

GREEN, 361a5a7: all pass; the dials return after 1.00 s (DialTimeout 1 s), the cluster ones after 0.50 s (DialTimeout 500 ms).

--- PASS: TestDialHandshakeIsBoundedByDialTimeout1205 (0.00s)
    --- PASS: TestDialHandshakeIsBoundedByDialTimeout1205/netconn (1.00s)
    --- PASS: TestDialHandshakeIsBoundedByDialTimeout1205/direct (1.00s)
    --- PASS: TestDialHandshakeIsBoundedByDialTimeout1205/eventloop (1.00s)
--- PASS: TestDialHandshakeKeepsTheCallersShorterDeadline1205 (0.20s)
--- PASS: TestClientSubscribeAndDoBoundTheHandshake1205 (0.00s)
    --- PASS: TestClientSubscribeAndDoBoundTheHandshake1205/Do (1.00s)
    --- PASS: TestClientSubscribeAndDoBoundTheHandshake1205/Subscribe (1.00s)
--- PASS: TestClusterSubscribeBoundsTheHandshake1205 (0.00s)
    --- PASS: TestClusterSubscribeBoundsTheHandshake1205/Subscribe (0.50s)
    --- PASS: TestClusterSubscribeBoundsTheHandshake1205/SSubscribe (0.50s)
ok  	github.com/goceleris/celeris/driver/redis	2.902s
  • go test ./driver/redis -race -run 1205 -count=20: ok (macOS arm64, 55 s). The first run of this count=20 found a real flake in my first version (the direct path's socket deadline fires a hair before the context timer, so the "timed out" check saw no context error), fixed: the driver's bound is recognised by the socket deadline too.
  • Whole driver/redis package: -race -count=1 and plain -count=1, Linux (golang:1.27.3, --cpus 2): ok both (race 170 s, plain 174 s); -race -count=1 on macOS: ok (38 s).
  • golangci-lint v2.15.0, GOOS=linux: no finding in the files of this PR (two errcheck findings in close_after_teardown_859_linux_test.go:135 and pubsub_rig_engines_887_linux_test.go:77 exist on main too and are not touched here). GOOS=windows go vet ./driver/redis: clean.

Behaviour notes

  • Where it applies: every new conn's handshake, for commands and pub/sub (including the ForceRESP2 legacy-auth path and the SELECT). A conn is bounded by DialTimeout for the connect and, separately, by DialTimeout for the handshake: a slow connect followed by a silent HELLO takes up to about 2 x DialTimeout; the silent-node case in the issue takes about 1 x.
  • A caller's shorter deadline still wins, and its error is returned unchanged. The driver's own bound returns celeris-redis: handshake timed out after <d>: context deadline exceeded: <cause>, which matches context.DeadlineExceeded (and, on the direct path, the socket's os.ErrDeadlineExceeded) with errors.Is.
  • The reconnect loop keeps its own 5 s cap on a dial (connect + handshake), so a loop dial is bounded by whichever is shorter.
  • Not changed: a direct-mode conn (useDirect) honours only a context's deadline, not its cancellation, while it waits for a reply. That is older behaviour and untouched here (the test's watchdog does not wait for a blocked direct dial for that reason).
  • ClusterClient.Subscribe against a cluster whose every node is silent now fails after about one DialTimeout per node it tries (each node once), instead of never.

Release note

DialTimeout (default 5 s) now also bounds the connection handshake (HELLO/AUTH/SELECT, including the ForceRESP2 path) of every new conn, for commands and pub/sub: a node that accepts the connection and does not answer used to hold Subscribe, SSubscribe and Do until the caller's context ended, for ever under context.Background(). A server that takes longer than DialTimeout to answer the handshake now fails the dial, also when the caller's context has a longer deadline or none.

…Refs #1205)

A node that accepts TCP and never answers HELLO holds the dial, and so
Client.Subscribe, ClusterClient.Subscribe/SSubscribe and any Do that needs a
new conn, until the caller's context ends: for ever under
context.Background. These tests fail on main (all three dial paths, Client and
ClusterClient entry points).
DialTimeout only covered net.Dialer.DialContext. The handshake (HELLO, then
AUTH/SELECT) ran on the caller's context, so a node that accepts TCP and never
answers HELLO held ClusterClient.Subscribe, ClusterClient.SSubscribe,
Client.Subscribe and any Do that needs a new conn until the caller's context
ended: for ever under context.Background.

handshake now runs under context.WithTimeout(ctx, DialTimeout) (the 5 s default
when unset). All three dial paths (unix event loop, net.Conn loop, direct) and
so the pool's command and pub/sub dials end in handshake. A caller's shorter
deadline still wins and is reported as the caller's; the driver's bound is
reported as an error that matches context.DeadlineExceeded and keeps the
underlying cause in the chain. The DialTimeout docs (Config, ClusterConfig,
SentinelConfig, WithDialTimeout) now say it bounds the connect and, separately,
the handshake.
@FumingPower3925 FumingPower3925 added this to the v1.6.0 milestone Nov 1, 2026
@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@codecov

codecov Bot commented Nov 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…llation; say the timeout once (Refs #1205)

Review r4 of #1209:
- a client with DialTimeout unset still bounds a silent HELLO node: a test hook
  lowers the default so the test does not wait 5 s, and the bound is checked to
  be the default's;
- a caller that cancels during the handshake gets context.Canceled back at once,
  not the driver's bound, and the half-open conn is closed;
- the loop paths' error no longer repeats the context error ("handshake timed
  out after 1s: context deadline exceeded"); the direct path keeps its socket
  cause in the chain;
- the Client and ClusterClient entry-point tests assert the handshake timeout,
  not any error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant